/*
 * utils.c
 *
 * Common helper utilities.
 *
 * Copyright (C) 2021 Rinnegatamante
 * Copyright (C) 2022 Volodymyr Atamanenko
 *
 * This software may be modified and distributed under the terms
 * of the MIT license. See the LICENSE file for details.
 */

#include <psp2/io/stat.h>

#include <stdio.h>
#include <stdarg.h>
#include <string.h>

#include "utils.h"

// For check_kubridge
SceUID _vshKernelSearchModuleByName(const char *, int *);

void *retNULL(void)
{
    return NULL;
}

int ret0(void)
{
    return 0;
}

int ret1(void)
{
    return 1;
}

int retminus1(void)
{
    return -1;
}

void do_nothing(void)
{
    // Silence is golden.
}

int file_exists(const char *path)
{
    SceIoStat stat;
    return sceIoGetstat(path, &stat) >= 0;
}

int debugPrintf(char *text, ...)
{
#ifdef DEBUG
    va_list list;
    char string[4096];

    va_start(list, text);
    vsprintf(string, text, list);
    va_end(list);
    //fprintf(stderr, "%s", string);
    SceUID fd = sceIoOpen(LOG_PATH, SCE_O_WRONLY | SCE_O_CREAT | SCE_O_APPEND, 0777);
    if (fd >= 0)
    {
        sceIoWrite(fd, string, strlen(string));
        sceIoClose(fd);
    }
#endif
    return 0;
}

int check_kubridge(void)
{
    int search_unk[2];
    return _vshKernelSearchModuleByName("kubridge", search_unk);
}
